package com.canbot.u05.activity.customvoice.tool

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.Point
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View


import com.canbot.u05.R
import kotlinx.android.synthetic.main.custom_dialog_view1.*

/**
 * CustomDialogView
 *
 * @author Liu.Heng
 * @date 2019/5/31
 */
class CustomDialogView(context: Context, private val listener: OnCloseListener?, private val title: String?, private val content: String?) : Dialog(context) {

    interface OnCloseListener {

        /***
         * 点击按钮回调
         * @param dialog  mContext
         * @param confirm true or false
         */
        fun onClick(dialog: Dialog?, confirm: Boolean)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.custom_dialog_view1)
        setCanceledOnTouchOutside(false)
        /**设置弹窗的宽度*/
        val m = window?.windowManager
        val d = m?.defaultDisplay
        val p = window?.attributes
        val size = Point()
        d?.getSize(size)
        p?.width = size.x
        p?.height = size.y
        window?.attributes = p
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        hideBottomUIMenu()

        cancel_btn.setOnClickListener(::buttonEvent)
        affirm_btn.setOnClickListener(::buttonEvent)
        text.text = title
        text1.text = content
    }


    /**
     * 隐藏虚拟按键，并且全屏
     */
    private fun hideBottomUIMenu() {
        val attributes = window?.attributes
        attributes?.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        window?.attributes = attributes
        window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        window?.decorView?.setOnSystemUiVisibilityChangeListener { _ ->
            var uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    /**布局位于状态栏下方*/
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    /**全屏*/
                    View.SYSTEM_UI_FLAG_FULLSCREEN or
                    /**隐藏导航栏*/
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            uiOptions = uiOptions or 0x00001000
            window?.decorView?.systemUiVisibility = uiOptions
        }
    }

    private fun buttonEvent(view: View) {
        when (view.id) {
            R.id.cancel_btn -> {
                listener?.onClick(this, false)
                this.dismiss()
            }
            R.id.affirm_btn -> listener?.onClick(this, true)
            else -> {
            }
        }

    }
}


